package cn.tedu.dimanage.information.webapi.service;

import cn.tedu.dimanage.common.ex.ServiceException;
import cn.tedu.dimanage.common.web.State;
import cn.tedu.dimanage.information.service.IInformationService;
import cn.tedu.dimanage.information.webapi.controller.PhotoStorageController;
import cn.tedu.dimanage.information.webapi.mapper.InformationMapper;
import cn.tedu.dimanage.pojo.dto.InformationDTO;
import cn.tedu.dimanage.pojo.vo.InformationSimpleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class InformationServiceImpl implements IInformationService {

        @Autowired
        private InformationMapper mapper;

        @Autowired
        private PhotoStorageController photoStorageController;
        /**
         * 检查标题是否重复
         */
        @Override
        public void addNew(InformationDTO informationDTO){
                System.out.println("infomation"+informationDTO);
                String title = informationDTO.getTitle();
                InformationSimpleVO queryResult = mapper.selectByTitle(title);
                if (queryResult != null){
                        log.error("此标题已被其他用户使用");
                        throw new ServiceException(State.ERR_USER_NAME_DUPLICATE,"添加标题"+title+"失败,标题已存在");
                }
                log.trace("标题没有被占用,可以创建");

                String img = informationDTO.getImg();
                System.out.println("img"+img);
                log.debug("图片路径"+img);

                String content = informationDTO.getContent();
                log.debug("正文"+content);

                //创建时间
                informationDTO.setGmtCreate(LocalDateTime.now());
                //修改时间
                informationDTO.setGmtModified(null);

                int row = mapper.insert(informationDTO);
                if (row != 1){
                        log.error("创建失败,将抛出异常");
                        throw new ServiceException(State.ERR_INSERT,"添加咨询失败,服务器忙,请稍后再次尝试!");
                }

        }

        /**
         * 咨询管理页面--获取咨询管理页面的所有信息
         * @return
         */
        @Override
        public List<InformationSimpleVO> selectList(){
                return mapper.getListInformationVO();
        }


        /**
         * 咨询管理页面--删除对应的咨询信息
         * @param id
         */
        public void deleteById(Long id){

                log.info("进入删除方法");
                InformationSimpleVO query = mapper.selectById(id);
                String img = query.getImg();
                log.info("开始删除咨询业务");
                log.info("图片名为"+img);
                int row = mapper.deleteById(id);
                if (row != 1){
                        log.error("删除,将抛出异常");
                        throw new ServiceException(State.ERR_DELETE,"删除用户失败,服务器忙,请稍后再次尝试!");
                }
                //删除成功后删除图片

                photoStorageController.remove(img);
                log.info("我要删图片了");
        }

        /**
         * 咨询管理页面--查看咨询详情
         * @param id
         * @return
         */
        public InformationSimpleVO selectById(Long id){
                InformationSimpleVO query = mapper.selectById(id);
                return query;
        }

        /**
         * 咨询管理页面--修改咨询
         * @param informationDTO
         */

        public void updateById(InformationDTO informationDTO){
                Long id = informationDTO.getId();
                InformationSimpleVO query = mapper.selectById(id);
                String img = query.getImg();
                // 得到原始的img

                informationDTO.setGmtModified(LocalDateTime.now()); //插入当前修改时间
                int row = mapper.updateById(informationDTO);//修改
                photoStorageController.remove(img);

                if (row != 1){
                        log.error("修改,将抛出异常");
                        throw new ServiceException(State.ERR_UPDATE,"修改用户失败,服务器忙,请稍后再次尝试!");
                }
        }

        /**
         * 咨询管理页面 页头--通过标题查咨询
         * @param title
         * @return
         */
        public InformationSimpleVO selectByTitle(String title){
                InformationSimpleVO query = mapper.selectByTitle(title);
                return query;
        }


}
